package com.example.lock.controller;

import com.example.lock.zk.impl.ZkTemporaryOrderNodeLock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description: 模拟 zk 羊群效应效果
 * @Author: mingtian
 * @CreateDate: 2020/12/10 16:22
 * @Version: 1.0
 */
@RestController
public class ZkTaskServiceController {

    /**
     * 打印日志
     */
    private Logger logger = LoggerFactory.getLogger(ZkTaskServiceController.class);

    @Autowired
    private ZkTemporaryOrderNodeLock zkTemporaryOrderNodeLock;

    @GetMapping("/task")
    public String task() {
        try {
            // 获取锁
            zkTemporaryOrderNodeLock.getLock();
            // 执行业务逻辑
            logger.info("开始执行业务逻辑");
            // 释放锁
            zkTemporaryOrderNodeLock.releaseLock();
        } catch (Exception e) {
            logger.error("errorInfo:{}", e.getMessage());
        }
        return "success";
    }

}